package com.djinfo.work.controller;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.entity.*;
import com.djinfo.work.service.*;
import com.djinfo.work.util.activemq.MessageBean;
import com.djinfo.work.util.activemq.Receiver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.transport.Session;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

import static com.djinfo.work.util.CacheUtil.ajaxJson;

/**
 * 消息
 */
@Controller
@RequestMapping("/messages")
public class MessagesController {
	private static final Log log = LogFactory.getLog(MessagesController.class);
	/**
	 * 自动注入
	 */
	@Autowired
	private MessagesService messagesService;



	@Autowired DictionariesService dictionariesService;

	/**
	 * 消息列表
	 */
	@RequestMapping("/list.do")
	@ResponseBody
	public AjaxJson list(SysMessageEntity sysMessageEntity, Integer pagenum, Integer pagesize, HttpServletRequest request){
		String  id = (String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson = messagesService.list(sysMessageEntity,pagenum,pagesize,id);
		return  ajaxJson;
	}



	/**
	 * 我的消息列表
	 */
	@RequestMapping("/myList.do")
	@ResponseBody
	public AjaxJson myList(SysMessageEntity sysMessageEntity, Integer pagenum, Integer pagesize, HttpServletRequest request){
		String  id = (String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson = messagesService.myList(sysMessageEntity,pagenum,pagesize,id);
		return  ajaxJson;
	}


	/**
	 * 获取消息对象信息
	 */
	@RequestMapping("/redirect.do")
	public ModelAndView redirect(SysMessageEntity sysMessageEntity){
		ModelAndView modelAndView = messagesService.redirect(sysMessageEntity);
		return  modelAndView;
	}

	/**
	 * 保存
	 */
	@RequestMapping("/saveOrUpdate.do")
	@ResponseBody
	public AjaxJson save(SysMessageEntity sysMessageEntity, HttpSession session){
		AjaxJson ajaxJson = messagesService.save(sysMessageEntity, session);
		return ajaxJson;
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public AjaxJson update(SysMessageEntity sysMessageEntity, HttpSession session){
		AjaxJson ajaxJson =messagesService.update(sysMessageEntity,session);
		return ajaxJson;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public AjaxJson delete(SysMessageEntity sysMessageEntity){
		AjaxJson ajaxJson =messagesService.deletes(sysMessageEntity);
		return ajaxJson;
	}

	/**
	 * 批量删除
	 */
	@RequestMapping("/deleteSome.do")
	@ResponseBody
	public AjaxJson deleteSome(String[] ids){
		AjaxJson ajaxJson =messagesService.deleteSome(ids);
		return ajaxJson;
	}

	/**
	 *初始化数据
	 */
	@RequestMapping("/getInitInfo.do")
	@ResponseBody
	public AjaxJson getInitInfo(HttpSession session){
		AjaxJson ajaxJson =messagesService.getInitInfo(session);
		return  ajaxJson;
	}

	/**
	 *初始化数据
	 */
	@RequestMapping("/getmessagesInfo.do")
	@ResponseBody
	public AjaxJson getmessagesInfo(HttpSession session){
		AjaxJson ajaxJson = messagesService.getmessagesInfo(session);
		return  ajaxJson;
	}



	/**
	 *更新状态
	 */
	@RequestMapping("/updateStatus.do")
	@ResponseBody
	public AjaxJson updateStatus(String id){
		AjaxJson ajaxJson = messagesService.updateStatus(id);
		return  ajaxJson;
	}


	/**
	 * 获取消息对象信息
	 */
	@RequestMapping("/readMessage.do")
	public ModelAndView readMessage(SysMessageEntity sysMessageEntity,HttpServletRequest request){
		String  id = (String)request.getSession().getAttribute("userId");
		ModelAndView modelAndView = messagesService.updateReadMessage(sysMessageEntity,id);
		return  modelAndView;
	}

	/**
	 *更新状态
	 */
	@RequestMapping("/getMassage.do")
	@ResponseBody
	public AjaxJson getMassage(HttpServletRequest request){
		String  id = (String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson = messagesService.getMessageNum(id);
		return  ajaxJson;
	}

	/**
	 *30秒请求一次，获取30秒之内
	 */
	@RequestMapping("/getMassages.do")
	@ResponseBody
	public AjaxJson getMassages(HttpServletRequest request){
		String  id = (String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson = messagesService.getMessage(id);
		return  ajaxJson;
	}

	/**
	 *30秒请求一次，获取30秒之内
	 */
	@RequestMapping("/getMass.do")
	@ResponseBody
	public AjaxJson getMass(HttpServletRequest request){
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String, Object> map = new HashMap<>();
		String text = "";
		Object object = request.getSession().getAttribute("writer");
		MessageBean messageBean = null;
		if (object!=null){
			map.put("messageBean", messageBean);
			System.out.println("activemqB: " + text + "messageBean: " + messageBean);
		}else {
			text = Receiver.receive();
			if (!"".equals(text)) {
				Document document = null;
				try {
					document = DocumentHelper.parseText(text);
					//获取根节点
					Element root = document.getRootElement();
					Element element1 = root.element("id");
					String id = element1.getText();
					Element element2 = root.element("publish");
					String publish = element2.getText();
					Element element3 = root.element("title");
					String title = element3.getText();
					Element element4 = root.element("content");
					String content = element4.getText();
					Element element5 = root.element("receiveObject");
					String receiveObject = element5.getText();
					Element element6 = root.element("attachMent");
					String attachMent = element6.getText();
					Element element7 = root.element("status");
					String status = element7.getText();
					Element element8 = root.element("type");
					String type = element8.getText();
					Element element9 = root.element("imageShow");
					String imageShow = element9.getText();
					Element element10 = root.element("createTime");
					String createTime = element10.getText();
					Element element11 = root.element("updateTime");
					String updateTime = element11.getText();
					Element element12 = root.element("createUser");
					String createUser = element12.getText();
					Element element13 = root.element("updateUser");
					String updateUser = element13.getText();
					MessageBean messageBean1 = new MessageBean();
					messageBean1.setId(id);
					messageBean1.setPublish(publish);
					messageBean1.setTitle(title);
					messageBean1.setContent(content);
					messageBean1.setReceiveObject(receiveObject);
					messageBean1.setAttachMent(attachMent);
					messageBean1.setStatus(status);
					messageBean1.setType(type);
					messageBean1.setImageShow(imageShow);
					messageBean1.setCreateTime(createTime);
					messageBean1.setUpdateTime(updateTime);
					messageBean1.setCreateUser(createUser);
					messageBean1.setUpdateUser(updateUser);
					String str = messageBean1.getReceiveObject();
					String userid = (String) request.getSession().getAttribute("userId");
					if (str.indexOf(userid) != -1) {
						System.out.println("包含");
					} else {
						messageBean1 = null;
						System.out.println("不包含");

					}
					System.out.println("activemqA: " + text);
					map.put("messageBean", messageBean1);
				} catch (DocumentException e) {
					e.printStackTrace();
				}
			} else {
				map.put("messageBean", messageBean);
				System.out.println("activemqB: " + text + "messageBean: " + messageBean);
			}
		}
		ajaxJson.setResult(map);
		return  ajaxJson;
	}

	/**
	 * 获取消息对象信息
	 */
	@RequestMapping("/getmesinfo.do")
	public ModelAndView getmesinfo(String messageid,HttpServletRequest request){
		ModelAndView modelAndView = messagesService.editReadMessage(messageid);
		return  modelAndView;
	}
}
